package adb.group.controller;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DecimalFormat;
import java.util.Enumeration;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class AddTreatment
 */
public class AddTreatment extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AddTreatment() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String pa = null;
		pa = request.getParameter("pa").substring(0, 5);
		Enumeration<String> enumeration = request.getParameterNames();
		Statement statement = null;
		String treatmentID = null;
		Connection connection = null;
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
		    connection = (Connection) DriverManager
					.getConnection(
							"jdbc:oracle:thin:@localhost:1521:ORCL",
							"dbfp_dba", "dbfpdba");
		   statement = (Statement) connection
					.createStatement();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if(enumeration.hasMoreElements()){
			 
				ResultSet resultSet = null;
				try {
					resultSet = statement
							.executeQuery("select treatment_ID from treatment_t order by 1 desc");
					while (resultSet.next()) {
						treatmentID = resultSet.getString(1);
					
                   int count = Integer.parseInt(treatmentID.substring(1, 5));
                   DecimalFormat decimalFormat = new DecimalFormat("0000");
                   treatmentID = decimalFormat.format(++count);
                   treatmentID = "T" + treatmentID;
                   String query1 = "insert into treatment_t values('"
						+ treatmentID + "'," + "sysdate" + ",'" + pa
						+ "','"
						+ request.getSession().getAttribute("workID")
						+ "')";
                   statement.executeUpdate(query1);
}
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

		while (enumeration.hasMoreElements()) {
			String paraName = (String) enumeration.nextElement();
			if (paraName.startsWith("d")) {
				int k = Integer.parseInt(paraName.substring(1));
				if (k % 2 == 1) {
					String drugID = (String) request.getParameter(paraName);
					String quantity = (String) request
							.getParameter("d"+String.valueOf((k+1)));
                        String query2 = "insert into drug_treatment_t values('"
								+ drugID + "'," + quantity + ",'" + treatmentID
								+ "')";
						try {
							statement.executeUpdate(query2);
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					
				}
			} else if (paraName.startsWith("o")) {
				String operationID = (String) request.getParameter(paraName);
                   String query2 = "insert into operation_lab_treatment_t values('"
							+ operationID + "','" + treatmentID + "')";
					
					try {
						statement.executeUpdate(query2);
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				
			}
		}
		response.sendRedirect("/project/resources/physician/physicianmain.jsp");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
